#ifndef __MOTOR_DRIVER_H
#define __MOTOR_DRIVER_H
#include "main.h"
#include <stdbool.h>

#define RPM_TO_RAD_PER_SEC (2.0f * 3.141593265358979 / 60.0f)
#define kMotorPn 10.0f


void MotorDriverInit(void);

extern void MotorDriverIabcUpdata(void);

// 偏移量结构体定义
typedef struct {
    uint32_t IA1_Offset;
    uint32_t IB1_Offset;
    uint32_t IC1_Offset;
} ADC_OffsetValues;

extern ADC_OffsetValues offsets;
typedef struct 
{
	  float wr;  //电角速度
    float vbus;                 //母线电压 V
    float iabc_now_A[3];        //三相实际电流 A
	  float udq_tar_V[2];
		float udq_now_V[2];
    float uabc_tar_V[3];        //三相目标电压 V 补偿之前
    float uabc_tar_com_V[3];    //死区补偿后的电压 V
    uint32_t encoder_cnt;       //编码器计数值 pulse
    int64_t encoder_sum;        //编码器累计值 pulse
    uint32_t hall_state;        //霍尔状态  0~7
    float elec_angle_bias;      // 电角度偏移值 0~2pi
    uint8_t elec_angle_bias_cal;  //电角度偏移值校准标志位 0未校准  1校准完成
		uint8_t current_offset_cal;
		uint16_t current_offset_cal_cnt;
}MotorDriver;

extern MotorDriver kMotorDriver;
typedef struct 
{
	uint32_t TIM01;  
	uint32_t TIM01_max;  
	uint32_t encoder_rxcnt;      
	uint32_t encoder_rxcnt_max;  
	uint32_t encoder_txcnt;      
	uint32_t encoder_txcnt_max;  
	
	uint32_t error;
	
}debugget_t;
extern debugget_t debugget;

bool ADC_CalculateOffset(ADC_HandleTypeDef* hadc1, ADC_HandleTypeDef* hadc2, ADC_OffsetValues* pOffset);
float MotorDriver_GetUd(void);
float MotorDriver_GetUq(void);


void MotorDriver_SetIdqTar(float id, float iq);





#endif